package org.brisling.sysman.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.LongUtil;
import org.brisling.sysman.domain.GroupCustomDetail;
import org.brisling.sysman.repository.GroupCustomDetailRepo;
import org.brisling.sysman.service.base.BaseSysmanTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/groupCustomDetail")
/**
 * 
 * <p>Title: GroupCustomDetailService</p>
 * <p>Description: 自定义用户组明细后台服务类</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年1月8日 下午2:20:15
 */
public class GroupCustomDetailService extends BaseSysmanTypeService<GroupCustomDetail>{
	
	
	@Autowired
	private GroupCustomDetailRepo baseRepo;
	
	public GroupCustomDetailService(){
		this.moduleName = ConstSysParam.MODULE_GROUPCUSTOM_DETAIL;
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<GroupCustomDetail> getGroupDetailListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
				
		return super.getPagedList(page, rows);
			
	}
	
	
	
	/**
	 * 分页查询明细列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<GroupCustomDetail> getGroupDetailList(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	/**
	 * 根据组名称查找组成员
	 * @param GroupDetailNames
	 * @return
	 */
	@RequestMapping("/getGroupDetailListByGroupid.action")
	public List<GroupCustomDetail> getGroupDetailListByGroupid(@RequestBody String body){
		
		return super.getPagedListByTypeId(body);
			
	}
	
	/**
	 * 根据组名称查找组成员
	 * @param GroupDetailNames
	 * @return
	 */
	@RequestMapping("/getListDomainByGroupid.action")
	public ListDomain<GroupCustomDetail> getListDomainByGroupid(@RequestBody String body){
		
		return super.getPagedListDomainByTypeId(body);
			
	}
	
	
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	/**
	 * 保存群组明细数据
	 * @param id
	 */
	@RequestMapping("/save.action")
	public GroupCustomDetail save(@RequestBody String body){
		GroupCustomDetail GroupDetail = bodyToObj(body, GroupCustomDetail.class);		
		super.save(GroupDetail);
		return GroupDetail;
	}
	
	/**
	 * 保存群组明细信息
	 * @param typeId	群组编码
	 * @param userId	选中的群组成员id
	 * @return
	 */
	@RequestMapping("/saveList.action")
	public List<GroupCustomDetail> saveList(@RequestParam(value="typeId",required=false) Long typeId,
			@RequestParam(value="userId",required=false) String userId){
		
		List<GroupCustomDetail> ret = null;
		if(typeId!=null && userId!=null){
			
			String[] uid = userId.split(",");
			List<GroupCustomDetail> existList = baseRepo.findByTypeId(typeId, defaultSort);
			
			//缓存前端传入的用户id
			Map<Long,Long> newMap = new HashMap<Long,Long>();
			for(String _uid:uid){
				Long lid = LongUtil.getLongValue(_uid, null);
				if(lid!=null){
					newMap.put(lid, lid);
				}
			}
			
			//判断已经存在的用户id
			if(existList!=null){
				for(GroupCustomDetail g:existList){
					//循环迭代比对传入用户id和已有用户id
					Long existId = g.getUserId();
					if(g!=null && existId!=null){
						if(newMap.containsKey(existId)){
							//传入用户已经存在，删除传入用户缓存数据
							newMap.remove(existId);							
						}
					}
				}
			}
			
			//判断是否存在新增用户信息
			if(newMap!=null && newMap.size()>0){
				for(Long nid:newMap.keySet()){
					GroupCustomDetail _g = new GroupCustomDetail();
					
					_g.setUserId(nid);
					_g.setTypeId(typeId);
					existList.add(_g);
				}
			}
			
			if(existList!=null && existList.size()>0){
				super.saveList(existList);
			}
			ret = baseRepo.findByTypeId(typeId, defaultSort);
		}
			
		
		return  ret;
	}
	
	
}
